package board.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import board.service.BoardService;
import board.to.GeneratorTO;
import board.to.P1TO;

public class BoardController extends MultiActionController {
	private BoardService service;
	public BoardController(BoardService service){
		this.service = service;
	}
	public ModelAndView start(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			Thread.sleep(1000);
		}catch(Exception e){}
		ModelAndView mv = null;
		GeneratorTO gto = new GeneratorTO();
		int stop = service.selectgtseqStop();
		int result = service.selectgtseq();
		int a = (int)(Math.random()*10)+1;

		gto.setGenerate(a);
		gto.setResult(result);
		service.insertGenerator(gto);
		service.updategtseq();

		HashMap map = new HashMap();
		map.put("a",a);
		map.put("stop",stop);
		mv = new ModelAndView("jsonView","map",map);
		return mv;
	}
	public ModelAndView startstopYes(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		service.updategtseqStopYes();
		mv = new ModelAndView("jsonView");
		return mv;
	}
	public ModelAndView startstopNo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		service.updategtseqStopNo();
		mv = new ModelAndView("jsonView");
		return mv;
	}
	public ModelAndView selectP1Status(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		int status = service.selectp1Status();
		mv = new ModelAndView("jsonView","status",status);
		return mv;
	}
	public ModelAndView updateP1Status0(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		service.updatep1Status0();
		mv = new ModelAndView("jsonView");
		return mv;
	}
	public ModelAndView updateP1Status1(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		service.updatep1Status1();
		mv = new ModelAndView("jsonView");
		return mv;
	}
	public ModelAndView p1(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			Thread.sleep(3000);
		}catch(Exception e){}
		ModelAndView mv = null;
		GeneratorTO b = null;
		//st select한담에 st가 1이면 p1seq를 +1한 다음 st를 0으로 업데이트
		P1TO p1to = new P1TO();
		int stop = service.selectP1seqStop();
		int result = service.selectP1seq();
		b = service.selectGenerator(result);
		if(b!=null){
		p1to.setResult(result);
		p1to.setP1(b.getGenerate());
		service.insertP1(p1to);
		service.deleteGenerator(result);
		service.updateP1seq();
		}
		HashMap map = new HashMap();
		map.put("b",b);
		map.put("stop",stop);
		mv = new ModelAndView("jsonView","map",map);
		return mv;
	}
	public ModelAndView p1StopNo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		service.updateP1seqStopNo();
		service.updatep1Status0();
		mv = new ModelAndView("jsonView");
		return mv;
	}
	public ModelAndView p1Status0(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		service.updatep1Status0();
		mv = new ModelAndView("jsonView");
		return mv;
	}
	public ModelAndView p1StopYes(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		service.updateP1seqStopYes();
		mv = new ModelAndView("jsonView");
		return mv;
	}
	public ModelAndView p2(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			Thread.sleep(5000);
		}catch(Exception e){}
		ModelAndView mv = null;
		P1TO b = null;
		int result = service.selectP2seq();
		b = service.selectP1(result);
		if(b!=null){
			service.deleteP1(result);
			service.updateP2seq();
		}
		mv = new ModelAndView("jsonView","b",b);
		return mv;
	}
	public ModelAndView p2stop(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			Thread.sleep(5000);
		}catch(Exception e){}
		ModelAndView mv = null;
		P1TO b = null;
		int results = service.selectP2seq();
		int result = results;
		b = service.selectP1(result);
		if(b!=null){
			service.deleteP1(result);
			service.updateP2seq();
		}
		mv = new ModelAndView("jsonView","b",b);
		return mv;
	}
}
